#!/usr/bin/env	python

from Crypto.Cipher import AES
import base64
import MySQLdb

class aes_crypto:

    def __init__(self):
        self.key = '2819232310abcdef'

    def encrypt_aes(self,text):
        aes = AES.new(self.key,AES.MODE_CBC)
        rem = len(text)%16
        if rem == 0:text+='x08'*16
        else:text+=chr(16-rem)*(16-rem)
        return aes.encrypt(text)

    def decrypt_aes(self,text):
        aes = AES.new(self.key,AES.MODE_CBC)
        text = aes.decrypt(text)
        pad = ord(text[-1])
        if pad == 'x08':return text[:-16]
        return text[:-pad]


if __name__ == "__main__":
    conn = MySQLdb.connect(host='xx',user='xx',passwd='xx',charset='utf8')
    cur = conn.cursor()

    a = aes_crypto()
    #p = a.encrypt_aes('sis%(l#!')
    #p1 = base64.encodestring(p).split()
    #print p1

    #sql = '''update oms.auth_ldap set pwd="%s" where user="westos"'''
    #cur.execute(sql %p1[0])

    sql = "select pwd from oms.auth_ldap where user='westos'"
    cur.execute(sql)
    pwd = cur.fetchall()[0][0]
    pw = a.decrypt_aes(base64.decodestring(pwd))
    print pw
